﻿using Harmontronics.Api.Config;
using Harmontronics.IService;
using Harmontronics.Model.Dto.Login;
using Harmontronics.Model.Dto.Menu;
using Harmontronics.Model.Dto.Role;
using Harmontronics.Model.Entitys;
using Harmontronics.Model.Other;
using Harmontronics.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Org.BouncyCastle.Asn1.Cms;
using System.Security.Cryptography;

namespace Harmontronics.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class MenuController : ControllerBase
    {
        private readonly IMenuService _menuService;
        private readonly IUnitOfWork _unitOfWork;

        public MenuController(IMenuService Menu, IUnitOfWork unitOfWork)
        {
            _menuService = Menu;
            _unitOfWork = unitOfWork;
        }
        [HttpPost]
        public ApiResponseData GetMenuData(MenuGet getData)
        {
            return ResultHelper.Success(_menuService.GetAllMenus(getData));
        }
        //获取所有已启用的
        [HttpGet]
        public ApiResponseData GetAllMenuTreeSelect()
        {
            return ResultHelper.Success(new
            {
                menus = _menuService.GetAllAbleMenus()
            });
        }
        [HttpPost]
        public ApiResponseData AddMenu(MenuAdd menuAdd)
        {   
            _menuService.AddMenu(menuAdd);
            if (_unitOfWork.Commit() > 0)
            {
                return ResultHelper.Success("", "Add Success!");
            }
            return ResultHelper.Error(500, "Add Failure");
        }
        [HttpGet]
        public ApiResponseData GetMenuTreeSelect(int roleId)
        {
            return ResultHelper.Success(new
            {
                menus = _menuService.GetAllAbleMenus(),
                checkKeys = _menuService.CollectMenuIds(new List<int>(), _menuService.GetAllMenusByRoleId(roleId))
            });
        }
        [HttpPost]
        public ApiResponseData UpdateMenuData(MenuEdit menuEdit)
        {
            _menuService.UpdateMenu(menuEdit);
            if (_unitOfWork.Commit() > 0)
            {
                return ResultHelper.Success("", "Update Success!");
            }
            return ResultHelper.Error(500, "Update Failure");
        }
        [HttpPost]
        public ApiResponseData DeleteMenu(DeleteIntId id)
        {
            _menuService.DeleteMenuById(id.id);
            if (_unitOfWork.Commit() > 0)
            {
                return ResultHelper.Success("", "Delete Success!");
            }
            return ResultHelper.Error(500, "Delete Failure");
        }
        [HttpPost]
        public ApiResponseData BatchDelMenu(List<int> ids)
        {
            _menuService.BatchDelMenu(ids);
            if (_unitOfWork.Commit() > 0)
            {
                return ResultHelper.Success("", "Delete Success!");
            }
            return ResultHelper.Error(500, "Delete Failure");
        }
        [HttpPost]
        public ApiResponseData ChangeMenuStatus(UpdateMenuStatus update)
        {
            MenuEdit menuEdit = new MenuEdit()
            {
                Id = update.Id,
                KeepAlive = update.KeepAlive
            };
            _menuService.UpdateMenu(menuEdit);
            if (_unitOfWork.Commit() > 0)
            {
                return ResultHelper.Success("", "Operate Success!");
            }
            return ResultHelper.Error(500, "Operate Failure!");
        }
    }
}
